package cn.openea.perpartner.componet.rabbit.service.impl.sms;

import cn.openea.perpartner.componet.web.entity.dto.sms.BatchDto;
import cn.openea.perpartner.componet.web.service.ISmsService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author Weird077
 * @description TODO
 * @date 2023-05-25 12:07
 */
@Slf4j
@Component
public class AliyunSmsConsumer {
    @Autowired
    private ISmsService smsService;
    @RabbitListener(queues = "${rabbit.aliyun.queue}")
    public void receiveBusiness(JSONObject object, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) throws IOException {
        BatchDto smsDto = JSON.toJavaObject(object, BatchDto.class);
        try {
            //执行发送短信
            smsService.executeSendBatchMsg(smsDto);
            //消息消费成功，手动确认消息,消息从队列中删除
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            log.error(e.getMessage());
            //消息消费失败，手动拒绝消息,消息从队列中删除
            channel.basicNack(deliveryTag, false, false);
        }
    }
}
